#!/usr/bin/env bash

source "$rvm_scripts_path/functions/pkg"

requirements_synology_lib_installed()
{
  ipkg list_installed | __rvm_grep "^$1 - " >/dev/null 2>&1 || return $?
}

requirements_synology_lib_available()
{
  ipkg list | __rvm_grep "^$1 - " >/dev/null 2>&1 || return $?
}

requirements_synology_libs_install()
{
  __rvm_try_sudo ipkg add -z "$@" || return $?
}

requirements_synology_install_custom()
{
  requirements_rvm_pkg_libs_install "$@" || return $?
}

requirements_synology_check_binaries()
{
  \typeset -a __missing_binaries
  __missing_binaries=()
  while
    (( $# ))
  do
    __rvm_which $1 >/dev/null || __missing_binaries+=( $1 )
    shift
  done
  if
    (( ${#__missing_binaries[@]} ))
  then
    rvm_error "Missing required binaries: ${__missing_binaries[*]} - make sure those are installed to continue."
    return ${#__missing_binaries[@]}
  fi
  true
}

requirements_synology_define()
{
  case "$1" in
    (rvm)
      true
      ;;

    (jruby*)
      requirements_check make

      if
        is_head_or_disable_binary "$1"
      then
        case $( jruby_installation_method  "$1" ) in
          ant) requirements_synology_check_binaries java git ant || return $? ;;
          mvn) requirements_synology_check_binaries java git mvn || return $? ;;
        esac
      else
        requirements_synology_check_binaries java || return $?
      fi
      ;;

    (ir*)
      requirements_synology_check_binaries mono || return $?
      ;;

    (opal)
      requirements_synology_check_binaries node || return $?
      ;;

    (*-head)
      requirements_check git
      requirements_synology_define "${1%-head}"
      ;;

    (*)
      requirements_check gcc autoconf automake libtool bison readline openssl
      requirements_rvm_pkg_lib_installed_custom yaml
      __rvm_update_configure_opt_dir "$1" "/usr/local"
      ;;
  esac
}
